package ystp.service;


import org.springframework.stereotype.Service;
import ystp.dao.KangjunyaowuMapper;
import ystp.dao.TongkeyaopinMapper;
import ystp.model.Kangjunyaowu;
import ystp.model.KangjunyaowuExample;
import ystp.model.Tongkeyaopin;
import ystp.model.TongkeyaopinExample;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class TongkeyaopinService {

    @Resource
    TongkeyaopinMapper ddMapper;

    public List<Map<String, Object>> findTongkeBiaojueByXiangmuId(String ppxiangmuId,String ppUserId,int ppPageIndex, int ppPageSize){
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        return ddMapper.findTongkeBiaojueByXiangmuId(ppxiangmuId,ppUserId,mmStartIndex,ppPageSize);
    }

    public List<Map<String, Object>> findTongkeyaopinByXiangmuId(String ppxiangmuId,int ppPageIndex, int ppPageSize){
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        return ddMapper.findTongkeyaopinByXiangmuId(ppxiangmuId,mmStartIndex,ppPageSize);
    }

    public List<Map<String, Object>> findTongkeyaopinResultByXiangmuId(String ppxiangmuId){
        return ddMapper.findTongkeyaopinResultByXiangmuId(ppxiangmuId);
    }

    public Integer getTongkeCount(String ppXiangmuId,String ppdrugname,String ppjiyao,String pptongguopingjia,
                                  String ppjicaiyao,String ppyuxuanyijian) {
        Object mmCount = ddMapper.countTongkeByXiangmuId(ppXiangmuId,ppdrugname,ppjiyao,pptongguopingjia,ppjicaiyao,ppyuxuanyijian);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public List<Map<String, Object>> findResultByXiangmuIdAndUserid(String ppxiangmuId,String ppuserid,
                                                                    String ppdrugname,String ppjiyao,String pptongguopingjia,
                                                                    String ppjicaiyao,String ppyuxuanyijian,
                                                                    int ppPageIndex, int ppPageSize){
        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        return ddMapper.findResultByXiangmuIdAndUserid(ppxiangmuId,ppuserid,ppdrugname,ppjiyao,pptongguopingjia,ppjicaiyao,ppyuxuanyijian,mmStartIndex,ppPageSize);
    }


    public boolean isexistByxiangmuId(String xiangmuId){
        TongkeyaopinExample mmExample=new TongkeyaopinExample();
        TongkeyaopinExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.selectByExample(mmExample).size()>0;
    }

    public Tongkeyaopin findOne(String id){
        return ddMapper.selectByPrimaryKey(id);
    }

    public List<Tongkeyaopin> findAllByXiangmuId(String xiangmuId){
        TongkeyaopinExample mmExample=new TongkeyaopinExample();
        TongkeyaopinExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return ddMapper.selectByExample(mmExample);
    }


    public List<Tongkeyaopin> findAllByXiangmuIdAndGuizebianhao(String xiangmuId,String guizebianhao){
        TongkeyaopinExample mmExample=new TongkeyaopinExample();
        TongkeyaopinExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId).andGuizebianhaoEqualTo(guizebianhao);
        return ddMapper.selectByExample(mmExample);
    }

    public List<Tongkeyaopin> findAllByXiangmuIdAndJicaiyao(String xiangmuId){
        TongkeyaopinExample mmExample=new TongkeyaopinExample();
        TongkeyaopinExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId).andJicaiyaoEqualTo("是");
        return ddMapper.selectByExample(mmExample);
    }

    public boolean add (Tongkeyaopin ddTongkeyaopin){
        return ddMapper.insert(ddTongkeyaopin)==1;
    }

    public boolean deleteByxiangmuId(String xiangmuId){
        TongkeyaopinExample mmExample=new TongkeyaopinExample();
        TongkeyaopinExample.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.deleteByExample(mmExample)>=0;
    }

    public List<Map<String, Object>> findGuizebianhaoByXiangmuId(String ppxiangmuId){
        return ddMapper.findGuizebianhaoByXiangmuId(ppxiangmuId);
    }

}
